﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Routing;
using Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Http;
using Utility.Extensions;

namespace QuickFramework.UICore.Controllers.Books
{
    /// <summary>
    /// 作者相关
    /// </summary>
    [Area("Books")]
    public class BookAuthorsController : Shared.Contracts.AuthorizeControllerBase
    {
        BookStores.IDomain.IBookAuthors m_IBookAuthors;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="bookAuthors"></param>
        public BookAuthorsController(BookStores.IDomain.IBookAuthors bookAuthors)
        {
            m_IBookAuthors = bookAuthors;
        }
        /// <summary>
        /// 根据用户名获取作者列表
        /// url: /api/Books/BookAuthors/GetBookAuthors
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetBookAuthors(string name, int pageIndex = 1, int pageSize = 10)
        {
            var items = await m_IBookAuthors.GetBookAuthors(name, pageIndex, pageSize);
            return Ok(items.Item1);
        }
        /// <summary>
        /// 根据用户名获取作者列表
        /// url: /api/Books/BookAuthors/GetBookAuthors4Edit
        /// </summary>
        /// <param name="keyword"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> GetBookAuthors4Edit(string keyword, int pageIndex = 1, int pageSize = 10)
        {
            var result = await m_IBookAuthors.GetBookAuthors(keyword, pageIndex, pageSize);
            return Ok(new { items = result.Item1, total = result.Item2 });
        }
        /// <summary>
        /// 存储作者信息
        /// url: /api/Books/BookAuthors/Save
        /// </summary>
        /// <param name="bookAuthors"></param>
        /// <returns>
        /// </returns>
        [HttpPost]
        public async Task<IActionResult> Save(BookStores.Entity.BookAuthors bookAuthors)
        {
            var isEdit = !string.IsNullOrEmpty(bookAuthors.ID);
            bool result = false;
            if (isEdit)
            {
                result = await m_IBookAuthors.Update(bookAuthors);
            }
            else
            {
                bookAuthors.ID = bookAuthors.NewKey;
                result = await m_IBookAuthors.Insert(bookAuthors);
            }
            return Ok(new { Result = result, Msg = bookAuthors.ID });
        }
        /// <summary>
        /// 删除作者信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Delete(string id)
        {
            return Ok(await m_IBookAuthors.Delete(id));
        }
    }
}
